﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.cusSupplier
{
    public partial class Edit : EditPagebase<ISupplierContract, Supplier>
    {
        public ISupplierTypeContract SupplierTypeContract { get; set; }
        public IWarehouseContract WarehouseContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIBindList.BindWhetherOrNot(ed_Enable, true);
                //FineUIListHelper.BindDDL<SupplierType>(SupplierTypeContract.GetResolvedTree(), 0L, ed_SupplierType,true);
                FineUIListHelper.BindDDL(SupplierTypeContract.EntityName, this.SupplierTypeContract.GetSelectList(), 0L, ed_SupplierType,true);
                //FineUIListHelper.BindDDL<Warehouse>(WarehouseContract.GetResolvedTree(), 0L, ed_Warehouse);
                //FineUIListHelper.BindDDL(WarehouseContract.EntityName, this.WarehouseContract.GetSelectList(), 0L, ed_Warehouse);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(Supplier entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_Code.SetValue(entity.Code);
            ed_ShortName.SetValue(entity.ShortName);
            if (!entity.SupplierTypeId.IsNull())
                ed_SupplierType.SetValue(entity.SupplierTypeId);
            ed_Tel.SetValue(entity.Tel);
            ed_Mobile.SetValue(entity.Mobile);
            ed_QQ.SetValue(entity.QQ);
            ed_WebChat.SetValue(entity.WebChat);
            ed_WebSite.SetValue(entity.WebSite);
            ed_Fax.SetValue(entity.Fax);
            ed_LegalPerson.SetValue(entity.LegalPerson);
            if (entity.Warehouse!=null)
            {
                ed_Warehouse.Values=new string[] { entity.Warehouse.Id.ToString() };
                ed_Warehouse.Text=entity.Warehouse.Name;
            }
            ed_RegMoney.SetValue(entity.RegMoney);
            ed_Address.SetValue(entity.Address);
            ed_License.SetValue(entity.License);
            ed_Enable.SetValue(entity.Enable);
            ed_AccountPeriod.SetValue(entity.AccountPeriod);
            ed_Remark.SetValue(entity.Remark);
        }

        protected override OperationResult SaveData(Supplier entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            entity.Code = ed_Code.GetValue().ToString();
            entity.ShortName = ed_ShortName.GetValue().ToString();
            if (ed_SupplierType.GetValue().ToLong() == 0L)
                entity.SupplierTypeId = 0L;
            else
                entity.SupplierTypeId = ed_SupplierType.GetValue().ToLong();
            entity.Tel = ed_Tel.GetValue().ToString();
            entity.Mobile = ed_Mobile.GetValue().ToString();
            entity.QQ = ed_QQ.GetValue().ToString();
            entity.WebChat = ed_WebChat.GetValue().ToString();
            entity.WebSite = ed_WebSite.GetValue().ToString();
            entity.Fax = ed_Fax.GetValue().ToString();
            entity.LegalPerson = ed_LegalPerson.GetValue().ToString();
            if (ed_Warehouse.Values.Count() > 0 && ed_Warehouse.Values[0].ToLong() >  0L)
                entity.WarehouseId = ed_Warehouse.Values[0].ToLong();
            else
                entity.WarehouseId = null;
            entity.RegMoney = ed_RegMoney.GetValue().ToDouble();
            entity.Address = ed_Address.GetValue().ToString();
            entity.License = ed_License.GetValue().ToString();
            entity.Enable = ed_Enable.GetValue().ToBool();
            entity.AccountPeriod = ed_AccountPeriod.GetValue().ToInt();
            entity.Remark = ed_Remark.GetValue().ToString();
            return base.SaveData(entity);
            
        }
    }
}
